﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;

namespace FDEE.Model
{
    public class TextOper
    {


         static String filePath = System.AppDomain.CurrentDomain.BaseDirectory + "OutData\\";

        //记录
        public static void WriteFile(int index, String coent, String prefix)
        {
            try
            {
                //创建文件夹
                creatFolder(filePath);
                //定义文件名，按每天一个文件
                String fileName = prefix + "-" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                //判断文件是否存在，如果无则创建
                if (!File.Exists(filePath + fileName) == true)
                {
                    FileStream fs = new FileStream(filePath + fileName, FileMode.Create);
                    StreamWriter sw = new StreamWriter(fs);
                    sw.WriteLine(index + "\t" + coent + "\t");
                    sw.Close();
                    fs.Close();
                }
                else
                {
                    StreamWriter sw = File.AppendText(filePath + fileName);
                    sw.WriteLine(index + "\t" + coent + "\t");
                    sw.Flush();
                    sw.Close();                   
                }
            }
            catch (Exception ex)
            {

            }
        }

            //创建文件夹
            public static void creatFolder(String path)
            {
                //不存在 则创建
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                    AddSecurityControll2Dic(path);
                }
                AddSecurityControll2Dic(path);
            }


            /// <summary>
            /// 为文件添加users，everyone用户组的完全控制权限
            /// </summary>
            /// <param name="filePath"></param>
            public static void AddSecurityControll2File(string filePath)
            {
                try
                {
                    //获取文件信息
                    FileInfo fileInfo = new FileInfo(filePath);
                    if (fileInfo != null)
                    {
                        return;
                    }

                    //获得该文件的访问权限
                    System.Security.AccessControl.FileSecurity fileSecurity = fileInfo.GetAccessControl();
                    //System.Windows.Forms.MessageBox.Show("0");
                    //添加ereryone用户组的访问权限规则 完全控制权限
                    fileSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
                    //添加Users用户组的访问权限规则 完全控制权限
                    fileSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow));
                    //设置访问权限
                    fileInfo.SetAccessControl(fileSecurity);
                }
                catch (Exception ex)
                {
                  

                }
            }
            /// <summary>
            /// 为文件夹添加users，everyone用户组的完全控制权限
            /// </summary>
            /// <param name="dirPath"></param>
            public static void AddSecurityControll2Dic(string dirPath)
            {
                try
                {
                   
                    ////设置访问权限
                    //dir.SetAccessControl(dirSecurity);
                    //获取文件夹信息
                    DirectoryInfo dir = new DirectoryInfo(dirPath);
                    //获得该文件夹的所有访问权限
                    System.Security.AccessControl.DirectorySecurity dirSecurity = dir.GetAccessControl(AccessControlSections.Access);
                    //设定文件ACL继承
                    InheritanceFlags inherits = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
                    //添加ereryone用户组的访问权限规则 完全控制权限
                    FileSystemAccessRule everyoneFileSystemAccessRule = new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow);
                    //添加Users用户组的访问权限规则 完全控制权限
                    FileSystemAccessRule usersFileSystemAccessRule = new FileSystemAccessRule("Users", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow);
                    bool isModified = false;
                    dirSecurity.ModifyAccessRule(AccessControlModification.Add, everyoneFileSystemAccessRule, out isModified);
                    dirSecurity.ModifyAccessRule(AccessControlModification.Add, usersFileSystemAccessRule, out isModified);
                    //设置访问权限
                    dir.SetAccessControl(dirSecurity);
                }
                catch (Exception ex)
                {
                   
                }
            }
       
    }
}
